<?php


namespace app\common\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\Db;

class RankReport extends Command
{

    protected function configure()
    {
        $this->setName('RankReport')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->setDescription('充值排行');
    }


    protected function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {
            $this->$action();
        }
    }

    /**
     * 代理排行
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function agent()
    {
        $id = Db::table("cms_manager_role")->where("alias", 'agent')->value('id');
        if (!$id) {
            return false;
        }
        $list = Db::table("cms_manager_user")->where("role_id", $id)->column('id');
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $day7 = $this->getDays(7);
        $day30 = $this->getDays(30);
        foreach ($list as $agent_id) {
            $data = [];
            $data['agent_id'] = $agent_id;
            $data['today_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("agent_id", $agent_id)->sum('total_fee');
            $data['today_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("user_reg_date", $today)->where("agent_id", $agent_id)->sum('total_fee');

            $data['yesterday_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("agent_id", $agent_id)->sum('total_fee');
            $data['yesterday_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("user_reg_date", $yesterday)->where("agent_id", $agent_id)->sum('total_fee');

            $data['amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day7)->where("agent_id", $agent_id)->sum('total_fee');
            $data['new_amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day7)->where("agent_id", $agent_id)->sum('total_fee');

            $data['amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day30)->where("agent_id", $agent_id)->sum('total_fee');
            $data['new_amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day30)->where("agent_id", $agent_id)->sum('total_fee');

            $data['amount'] = Db::table("pay_order_success")->where("status", 2)->where("agent_id", $agent_id)->sum('total_fee');

            $rank = Db::table("rp_recharge_rank_agent")->where("agent_id", $agent_id)->find();
            if ($rank) {
                Db::table("rp_recharge_rank_agent")->where("agent_id", $agent_id)->update($data);
            } else {
                Db::table("rp_recharge_rank_agent")->insert($data);
            }
        }
    }



    public function channel()
    {
        $id = Db::table("cms_manager_role")->where("alias", 'channel')->value('id');
        if (!$id) {
            return false;
        }
        $list = Db::table("cms_manager_user")->where("role_id", $id)->column('id');
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $day7 = $this->getDays(7);
        $day30 = $this->getDays(30);
        foreach ($list as $channel_id) {
            $data = [];
            $data['channel_id'] = $channel_id;
            $data['today_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("channel_id", $channel_id)->sum('total_fee');
            $data['today_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("user_reg_date", $today)->where("channel_id", $channel_id)->sum('total_fee');

            $data['yesterday_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("channel_id", $channel_id)->sum('total_fee');
            $data['yesterday_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("user_reg_date", $yesterday)->where("channel_id", $channel_id)->sum('total_fee');

            $data['amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day7)->where("channel_id", $channel_id)->sum('total_fee');
            $data['new_amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day7)->where("channel_id", $channel_id)->sum('total_fee');

            $data['amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day30)->where("channel_id", $channel_id)->sum('total_fee');
            $data['new_amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day30)->where("channel_id", $channel_id)->sum('total_fee');

            $data['amount'] = Db::table("pay_order_success")->where("status", 2)->where("channel_id", $channel_id)->sum('total_fee');

            $rank = Db::table("rp_recharge_rank_channel")->where("channel_id", $channel_id)->find();
            if ($rank) {
                Db::table("rp_recharge_rank_channel")->where("channel_id", $channel_id)->update($data);
            } else {
                Db::table("rp_recharge_rank_channel")->insert($data);
            }
        }
    }


    public function eb()
    {
        $id = Db::table("cms_manager_role")->where("alias", 'eb')->value('id');
        if (!$id) {
            return false;
        }
        $list = Db::table("cms_manager_user")->where("role_id", $id)->column('id');
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $day7 = $this->getDays(7);
        $day30 = $this->getDays(30);
        foreach ($list as $ebId) {
            $data = [];
            $data['eb_id'] = $ebId;
            $agentIdArr = Db::table("cms_manager_user")->where("parent_id",$ebId)->column("id");
            if ($agentIdArr){
                $data['today_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("agent_id", "in",$agentIdArr)->sum('total_fee');
                $data['today_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("user_reg_date", $today)->where("agent_id","in",$agentIdArr)->sum('total_fee');
                $data['yesterday_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("agent_id","in",$agentIdArr)->sum('total_fee');
                $data['yesterday_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("user_reg_date", $yesterday)->where("agent_id","in",$agentIdArr)->sum('total_fee');
                $data['amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day7)->where("agent_id","in",$agentIdArr)->sum('total_fee');
                $data['new_amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day7)->where("agent_id", "in",$agentIdArr)->sum('total_fee');
                $data['amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day30)->where("agent_id","in",$agentIdArr)->sum('total_fee');
                $data['new_amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day30)->where("agent_id", "in",$agentIdArr)->sum('total_fee');
                $data['amount'] = Db::table("pay_order_success")->where("status", 2)->where("agent_id", "in",$agentIdArr)->sum('total_fee');
            }else{
                $data['today_amount'] = 0;
                $data['today_new_amount'] = 0;
                $data['yesterday_amount'] = 0;
                $data['yesterday_new_amount'] = 0;
                $data['amount_7'] = 0;
                $data['new_amount_7'] = 0;
                $data['amount_30'] = 0;
                $data['new_amount_30'] =  0;
                $data['amount'] =  0;
            }
            $rank = Db::table("rp_recharge_rank_eb")->where("eb_id", "=",$ebId)->find();
            if ($rank) {
                Db::table("rp_recharge_rank_eb")->where("eb_id", "=",$ebId)->update($data);
            } else {
                Db::table("rp_recharge_rank_eb")->insert($data);
            }
        }
    }



    public function qa()
    {
        $list = Db::table("quick_app")->column('id');
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime("-1 day"));
        $day7 = $this->getDays(7);
        $day30 = $this->getDays(30);
        foreach ($list as $quick_id) {
            $data = [];
            $data['quick_id'] = $quick_id;
            $data['today_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("quick_id", $quick_id)->sum('total_fee');
            $data['today_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $today)->where("user_reg_date", $today)->where("quick_id", $quick_id)->sum('total_fee');

            $data['yesterday_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("quick_id", $quick_id)->sum('total_fee');
            $data['yesterday_new_amount'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", $yesterday)->where("user_reg_date", $yesterday)->where("quick_id", $quick_id)->sum('total_fee');

            $data['amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day7)->where("quick_id", $quick_id)->sum('total_fee');
            $data['new_amount_7'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day7)->where("quick_id", $quick_id)->sum('total_fee');

            $data['amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("create_date", "in", $day30)->where("quick_id", $quick_id)->sum('total_fee');
            $data['new_amount_30'] = Db::table("pay_order_success")->where("status", 2)->where("user_reg_date", "in", $day30)->where("quick_id", $quick_id)->sum('total_fee');

            $data['amount'] = Db::table("pay_order_success")->where("status", 2)->where("quick_id", $quick_id)->sum('total_fee');

            $rank = Db::table("rp_recharge_rank_qa")->where("quick_id", $quick_id)->find();
            if ($rank) {
                Db::table("rp_recharge_rank_qa")->where("quick_id", $quick_id)->update($data);
            } else {
                Db::table("rp_recharge_rank_qa")->insert($data);
            }
        }
    }

    /**
     * 获取日期数组
     * @param $num
     * @return array
     */
    private function getDays($num)
    {
        $dates = array();
        for ($i = $num - 1; $i >= 0; $i--) {
            $dates[] = date('Y-m-d', strtotime("-$i days"));
        }
        return $dates;
    }

}